/*     */ package com.iris.egrant.utils;
/*     */ 
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.security.spec.KeySpec;
/*     */ import javax.crypto.Cipher;
/*     */ import javax.crypto.SecretKey;
/*     */ import javax.crypto.SecretKeyFactory;
/*     */ import javax.crypto.spec.DESedeKeySpec;
/*     */ import org.apache.xmlbeans.impl.util.Base64;
import org.springframework.core.NestedRuntimeException;
/*     */ import org.springframework.util.Assert;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class EncryptionUtils
/*     */ {
/*     */   public static byte[] stringToByteArray(String input) {
/*  58 */     Assert.hasLength(input, "Input required");
/*     */     try {
/*  60 */       return input.getBytes("UTF-8");
/*  61 */     } catch (UnsupportedEncodingException fallbackToDefault) {
/*  62 */       return input.getBytes();
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String byteArrayToString(byte[] byteArray) {
/*  75 */     Assert.notNull(byteArray, "ByteArray required");
/*  76 */     Assert.isTrue((byteArray.length > 0), "ByteArray cannot be empty");
/*     */     try {
/*  78 */       return new String(byteArray, "UTF8");
/*  79 */     } catch (UnsupportedEncodingException e) {
/*  80 */       return new String(byteArray);
/*     */     } 
/*     */   }
/*     */   
/*     */   private static byte[] cipher(String key, byte[] passedBytes, int cipherMode) throws EncryptionException {
/*     */     try {
/*  86 */       KeySpec keySpec = new DESedeKeySpec(stringToByteArray(key));
/*  87 */       SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
/*  88 */       Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
/*  89 */       SecretKey secretKey = keyFactory.generateSecret(keySpec);
/*  90 */       cipher.init(cipherMode, secretKey);
/*  91 */       return cipher.doFinal(passedBytes);
/*  92 */     } catch (Exception e) {
/*  93 */       throw new EncryptionException(e.getMessage(), e);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String encrypt(String key, String inputString) throws EncryptionException {
/* 109 */     isValidKey(key);
/* 110 */     byte[] cipherText = cipher(key, stringToByteArray(inputString), 1);
/* 111 */     return byteArrayToString(Base64.encode(cipherText));
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] encrypt(String key, byte[] inputBytes) throws EncryptionException {
/* 126 */     isValidKey(key);
/* 127 */     return Base64.encode(cipher(key, inputBytes, 1));
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String decrypt(String key, String inputString) throws EncryptionException {
/* 142 */     Assert.hasText(key, "A key is required to attempt decryption");
/* 143 */     byte[] cipherText = cipher(key, Base64.decode(stringToByteArray(inputString)), 2);
/* 144 */     return byteArrayToString(cipherText);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] decrypt(String key, byte[] inputBytes) throws EncryptionException {
/* 159 */     Assert.hasText(key, "A key is required to attempt decryption");
/* 160 */     return cipher(key, Base64.decode(inputBytes), 2);
/*     */   }
/*     */   
/*     */   private static void isValidKey(String key) {
/* 164 */     Assert.hasText(key, "A key to perform the encryption is required");
/* 165 */     Assert.isTrue((key.length() >= 24), "Key must be at least 24 characters long");
/*     */   }
/*     */ 
/*     */   
/*     */   public static class EncryptionException
/*     */     extends NestedRuntimeException
/*     */   {
/*     */     private static final long serialVersionUID = 1L;
/*     */     
/*     */     public EncryptionException(String message, Throwable t) {
/* 175 */       super(message, t);
/*     */     }
/*     */     
/*     */     public EncryptionException(String message) {
/* 179 */       super(message);
/*     */     }
/*     */   }
/*     */ }


/* Location:              D:\文件kjt\WEB-INF\lib\egrant-core-2.2.20240205.1659.jar!\com\iris\egran\\utils\EncryptionUtils.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */